module.service('Activity', ['$rootScope', function ($rootScope) {
    var service = {
        _baseUrl: "/azeroth/back/activity/",
        activityList: [],
        currentActivity: {},
        totalSceneCount: 0,
        queryList: function (title, startTime, endTime,
                             currentPage, pageSize, callback) {

            var paramData = {
                title: title,
                //startTime: startTime,
                //endTime: endTime,
                pageNO: currentPage,
                pageSize: pageSize
            };
            $rootScope.ajaxRequestPost(service._baseUrl + "getActivityList.htm", paramData, function (resultJson) {
                angular.copy(resultJson.data, service.activityList);
                service.totalSceneCount = resultJson.totalCount;
                if (callback) {
                    callback();
                }
            });

        },
        insertActivity: function (title, activityType, startTime, endTime) {
            var paramData = {
                title: title,
                activityType: activityType,
                startTime: startTime,
                endTime: endTime
            };
            $rootScope.ajaxRequestPost(service._baseUrl + "insertActivity.htm", paramData, function (resultJson) {

                $.each(resultJson.data, function (key, value) {
                    service.imageList.push({'url': value});
                });
                service.totalSceneCount = resultJson.totalCount;
                if (callback) {
                    callback();
                }
            });
        },
        deleteActivity: function (id, callback) {
            var paramData = {
                id: id
            };
            $rootScope.ajaxRequestPost(service._baseUrl + "deleteActivity.htm", paramData, function (resultJson) {

                //根据id删除掉该数据
                var removeIndex;
                $.each(service.activityList, function (index, result) {
                    if (result['id'] == id) {
                        removeIndex = index;
                    }
                });
                service.activityList.splice(removeIndex, 1);

                if (callback) {
                    callback();
                }
            });
        },
        queryExtendList: function (activityId, callback) {

            var paramData = {
                activityId: activityId
            };
            $rootScope.ajaxRequestPost(service._baseUrl + "queryExtendList.htm", paramData, function (resultJson) {

                //解析json
                for (var index in resultJson.data) {
                    try {
                        resultJson.data[index].contents = JSON.parse(resultJson.data[index].contents);
                    } catch (e) {
                        resultJson.data[index].contents = [];
                    }
                }
                angular.copy(resultJson.data, service.activityExtendList);
                service.totalSceneCount = resultJson.totalCount;
                if (callback) {
                    callback();
                }
            });

        },
        deleteExtend: function (extendId, callback) {

            var paramData = {
                extendId: extendId
            };
            $rootScope.ajaxRequestPost(service._baseUrl + "deleteExtend.htm", paramData, function (resultJson) {

                //根据id删除掉该数据
                var removeIndex;
                $.each(service.activityExtendList, function (index, result) {
                    if (result['id'] == extendId) {
                        removeIndex = index;
                    }
                });
                service.activityExtendList.splice(removeIndex, 1);
                if (callback) {
                    callback();
                }
            });

        }

    };
    return service;
}]);
